/* 数据转换相关函数 */
import app from '@/config/app'
import http from '@/config/http'
import banks from './bank'

export default {
	date (text: string, format = 'mm-dd hh:MM', empty = false) {
		return text ? uni.$u.date(text, format) : (empty ? uni.$u.date('', format) : '')
	},
	ymd (text: string) {
		const arr = text.toString().split('-')
		let str = arr[0] + '年'
		arr[1] && (str += arr[1] + '月')
		arr[2] && (str += arr[2] + '日')
		return str
	},
	number (text: string, length = 2 as number) {
		if(isNaN(text)) return ''
    return length === -1 ? parseFloat(text) : parseFloat((text*1).toFixed(length))
  },
	km (text: string) {
		if(text < 1000){
			return parseInt(text) + '米'
		} else if (text > 100000){
			return '>100公里'
		} else {
			return parseFloat(text/1000).toFixed(1) + '公里'
		}
	},
  // 图片
  image (text: string) {
		if(typeof(text)!='string'||!text || text === '') {
			return '';
		} else if (text.startsWith('/static') || text.startsWith('http') || text.startsWith('blob') || text.startsWith('data:image/')) {
			return text
		}
    return app.imgRoot + text
  },
	// 相册
	gallery (list: []) {
		if(typeof(list) != 'object') return []
		return list.map(v => {
			const img = v.image || v.img || v.url || v
			return { ...v, image: (img.search(/^(\/static|http)/) === 0 ? '' : app.imgRoot) + img}
		})
	},
	// 
	icon (text: string) {
		// #ifdef APP-PLUS-NVUE
		return `\ue` + text
		// #endif
		// #ifndef APP-PLUS-NVUE
		return `&#xe` + text
		// #endif
	},
  // API接口
  api (text: string) {
		if(!text || text === '') {
			return '';
		} else if (text.startsWith('https//')) {
			return text
		}
    return http.baseUrl + '/' + text
  },
	// 外链
	link (text: string, params = []) {
		if(!text.startsWith('http//') && !text.startsWith('https//')) {
			text = app.host + text
		}
		// #ifdef H5
		return text  + '?' + params.join('&')
		// #endif
		// #ifndef H5
		return '/pages/public/web?url=' + text  + '?' + params.join('&')
		// #endif
	},
	// 层级数组值
	tier (text: string) {
		if(!['number', 'string'].includes(typeof(text))){
			return []
		}
		const code = String(text)
		return [code.substr(0, 2), code.substr(0, 4), code]
	},
	//银行
	bank (val=0) {
		return banks[val]
	},
	// 地区
	region (value = '', full = false) {
		if(!value){
			return ''
		}
		const tree = uni.$store.cache.regions
		const cs = String(value).match(/\d{2}/g) as string[]
		let item = tree.find((r:any)=>r.value==cs[0])
		// console.log(i1, a[0].padEnd(6, '0'))
		let text = item?.label
		if(cs.length === 1){
			return text
		}
		item = item?.children.find((r:any)=>r.value==cs[0]+cs[1])
		if(full){
			text += item?.label
		} else {
			text = item?.label
		}
		if(cs.length === 2){
			return text
		}
		item = item?.children.find((r:any)=>r.value==value)
		if(full){
			text += item?.label
		} else {
			text = item?.label
		}
		return text
	},
	// 城市
	bankCity (val = '', full = true) {
		if(!val){
			return ''
		}
		const item = uni.$store.cache.BankCities.find(r=>r.id==val)
		if(!item){
			return ''
		} else if ([1,2,3,4].includes(item.id) || !full) {
			return item.name
		} else {
			const parent = uni.$store.cache.BankCities.find(r=>r.id==item.pid)
			return parent.name + item.name
		}
	},
	// 国家
	country (value = 0, field = 'name') {
		const countrys = uni.$store.cache.countrys
		return countrys.find((r:{id:number})=>r.id==value)?.name ?? ''
	},
	// 分类
	cate (value: string = '', full = false) {
		if(!value){
			return ''
		}
		const tree = uni.$store.cache.cates
		const cs = String(value).match(/\d{2}/g) as string[];
		let item = tree.find((r:any)=>r.value==cs[0])
		// if(item == undefined) return ''
		let text = item?.label
		if(cs.length === 1){
			return text
		}
		item = item?.children.find((r:any)=>r.value==cs[0]+cs[1])
		// if(item == undefined) return text
		if(full){
			text += item?.label
		} else {
			text = item?.label
		}
		if(cs.length === 2){
			return text
		}
		item = item?.children.find((r:any)=>r.value==value)
		// if(item == undefined) return text
		if(full){
			text += item?.label
		} else {
			text = item?.label
		}
		return text
	},
	// 快贷公司
	expr (val: string = '') {
		if(!val){
			return ''
		}
		const exprs = uni.$store.cache.exprs
		const v = exprs[val]
		return v || ''
	},
	// radio, chekbox, select等的值
  label (text: string, options: object | array) {
    if (options && options[0] && typeof (options[0]) === 'string') {
      return options[text]
    } else if (options && options[text]) {
      return options[text].label
    } else {
      return ''
    }
  },
  empty (text: string, $empty = '空') {
    return text ? text : $empty
  },
  // 数组转换为option数组参数, radio, chekbox, select等会用到
  options (text: string | object) {
    let data = []
    if(typeof (text) === 'string' && text !== ''){
      data = JSON.parse(text)
    } else if (typeof (text) === 'object') {
      data = text
    }
    const options = []
    for (const i in data) {
      if (['string', 'number'].includes(typeof (data[i]))) {
        options.push({ label: data[i] , value: i + '' })
      } else {
        options.push(data[i])
      }
    }
    return options
  },
	// 手机格式化
	mobile (text: string) {
		return text.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
	},
	tabs (text: string, attr='name', allLabel) {
    let data = []
    if(typeof (text) === 'string' && text !== ''){
      data = JSON.parse(text)
    } else if (typeof (text) === 'object') {
      data = text
    }
    const tabs = []
		for(const i in data){
      if (typeof (data[i]) === 'string' || typeof (data[i]) === 'number') {
        tabs.push({ [attr]: data[i], value: parseInt(i) })
      } else {
        tabs.push(data[i])
      }			
		}
		return tabs
	},
	money (text: string) {//金额格式化
		return parseFloat(text).toFixed(2).toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,').replace(
			/\,$/, '').split('').reverse().join('');
	}
}
